/**
 * Copyright 2015 Qiyi Inc. All rights reserved.
 *
 * @file:   Statistic.Util.js
 * @path:   js-src/album/
 * @author: karkarrotxu@qiyi.com
 * @date:   2017-10-25
 */
qui(
    function (require, exports, module) {
        Statistic.Process = function (options) {
            this.regKey = options.regKey;
            this.logEntity = {
                logArr: [],
                push: function (logData) {
                    this.logArr.push(logData);
                }
            }
            this.isStop = false;
            this.statusDescr = null;
            this.timerId = -1;
            this.treeObj = {};
            this.methodObj = {};
            this.rootKey = null;
            this.currentKey = null;
            this.finalHandler = function(resultData){
                if( typeof (options.handler) ){
                    options.handler(resultData);
                }
            };
        }
        Com.inherits(Statistic.Process, {}).extend({
            startProcess: function () {
                console.log("[regKey=" + this.regKey + "]process start");
                var self = this;
                if (self.timerId != -1) {
                    clearInterval(self.timerId);
                }
                self.timerId = setInterval(function () {
                    if (!self.isStop && !self.isWork) {
                        self.callNext(self.currentKey)
                    }
                }, 300)
            },
            stopProcess: function () {
                clearInterval(this.timerId);
                this.isStop = true;
                this.isWork = false;
                console.log("[regKey=" + this.regKey + "]process over[status=" + this.statusDescr + "]");
                console.log(JSON.stringify(this.logEntity));
                this.finalHandler( {regKey:this.regKey,status:this.statusDescr} );
            },
            callNext: function (nodeKey) {
                console.log("callNext: " + nodeKey);
                var node = this.treeObj[nodeKey];

                if (node != null) {
                    var method = this.methodObj[nodeKey];
                    this.logEntity.push("[start node:" + nodeKey + "][" + Date.now() + "]");
                    this.isWork = true;
                    if (node.needResult != null) {
                        method.call(this, this.treeObj[node.needResult].result);
                    } else {
                        method.call();
                    }
                } else {
                    this.statusDescr = "complete";
                    this.stopProcess();
                }
            },
            addOrderFunObj: function (nodeKey, options) {
                var self = this;
                options = $.extend({}, {
                    judge: function (data) {
                        return true;
                    },
                    success: function (data) {
                        console.log(data);
                    },
                    error: function (data) {
                        console.log(data);
                    }
                }, options);
                var wrapHandler = function (data) {
                    self.logEntity.push("[end nodeKey:" + nodeKey + "][" + Date.now() + "]");
                    self.treeObj[nodeKey].result = data;
                    if (!options.judge(data)) {
                        self.logEntity.push("[interrupt nodeKey:" + nodeKey + "][" + JSON.stringify(data) + "]");
                        self.statusDescr = "interrupt";
                        self.stopProcess();
                    } else {
                        self.currentKey = self.treeObj[nodeKey].nextKey;
                    }
                    self.isWork = false;
                }

                var tempOptions = $.extend({}, options, {
                    success: function (data) {
                        options.success(data);
                        wrapHandler(data);
                    },
                    error: function (data) {
                        options.error(data);
                        wrapHandler(data);
                    }
                })
                self.methodObj[nodeKey] = function () {
                    var finalOptions = tempOptions;
                    var result = null;
                    if (finalOptions.needResult != null) {
                        if (finalOptions.needResult in self.treeObj) {
                            result = self.treeObj[finalOptions.needResult].result
                        }
                        if(typeof finalOptions.checkNeedResult == "function"){
                            if( !finalOptions.checkNeedResult( result )){
                                console.log("checkNeedResult fail" )
                                self.statusDescr = "interrupt";
                                self.stopProcess();
                                return ;
                            }
                        }
                    }
                    if ( typeof  options.beforeQuery == "function") {
                        finalOptions = options.beforeQuery.call(this, finalOptions, result);
                    }
                    $.ajax(finalOptions);
                }
            },
            addProcessTree: function (node) {
                this.treeObj[node.key] = node;
                if (node.root == true) {
                    this.rootKey = node.key;
                    this.currentKey = node.key;
                }
            }
        });
    }
);